package com.example.project.dao.impl;

import com.example.project.dao.ICpcDao;
import com.example.project.entity.Cpc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/*
编写者：王住泽
 */
@Component
public class ICpcDaoImpl implements ICpcDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Override
    public int insertCpc(Cpc cpc) {
        return jdbcTemplate.update("insert into cpc(Id,CpcId,CpcTime,CpcAge,CpcSalary )"+
                "Value(?,?,?,?,?)"
        ,cpc.getId()
        ,cpc.getCpcId()
        ,cpc.getCpcTime()
        ,cpc.getCpcAge()
        ,cpc.getCpcSalary()
        );
    }

    @Override
    public int deleteCpc(Cpc cpc) {
        return jdbcTemplate.update("delete from cpc "+
                "where CpcId=?",cpc.getId());
    }

    @Override
    public List<Cpc> getAllCpc() {
        return jdbcTemplate.query("select " +
                "Id,CpcId,CpcTime,CpcAge,CpcSalary " +
                "from Cpc",new Object[]{},
                new BeanPropertyRowMapper<>(Cpc.class));
    }
    @Override
    public List<Cpc> getAllCpc(Cpc cpc) {
        return jdbcTemplate.query("select " +
                "Id,CpcId,CpcTime,CpcAge,CpcSalary " +
                "from Cpc",new Object[]{},
                new BeanPropertyRowMapper<>(Cpc.class));
    }

    @Override
    public int updataCpc(Cpc cpc) {

        return jdbcTemplate.update("update cpc set " +
                        "Id=?,CpcTime=?,CpcAge=?,CpcSalary=?" +"where CpcId=?"
                ,cpc.getId()
                ,cpc.getCpcTime()
                ,cpc.getCpcAge()
                ,cpc.getCpcSalary()
                ,cpc.getCpcId());
    }

    @Override
    public Cpc getCpc(Cpc cpc) {
        List<Cpc> result=jdbcTemplate.query("select " +
                        "Id,CpcId,CpcTime,CpcAge,CpcSalary " +
                        "from cpc where CpcId='"+cpc.getCpcId()+"'",
                new Object[]{},
                new BeanPropertyRowMapper<>(Cpc.class));
        if (result.size()>=1)
            return result.get(0);
        else return null;
    }
}
